package black.market.main;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.ContentValues;
import android.util.Log;

public class XpathParser {
	private static final String TAG = "XpathParser";

	int i = 0;

	public XpathParser() {
		super();
	}

	public Document getDocument(String msg) {
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		domFactory.setIgnoringElementContentWhitespace(true);
		domFactory.setIgnoringComments(true);
		domFactory.setNamespaceAware(true); // never forget this!
		DocumentBuilder builder;
		Document doc = null;
		try {
			builder = domFactory.newDocumentBuilder();
			doc = builder.parse(new ByteArrayInputStream(msg.getBytes("utf-8")));
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return doc;
	}

	public XPath getXpath() {
		XPathFactory factory = XPathFactory.newInstance();
		return factory.newXPath();
	}

	public void xpathParserEvaluate(String msg, String reqExpression) {
		try {
			Document doc = getDocument(msg);
			XPath xPath = getXpath();

			NodeList nodeList = (NodeList) xPath.evaluate(reqExpression, doc, XPathConstants.NODESET);

			if (nodeList.getLength() > 0) {
				BlackPublic.debugLog(TAG, "xpathParserEvaluate");
				for (int i = 0; i < nodeList.getLength(); i++) {
					BlackPublic.debugLog(TAG, "Type: " + doc.getDocumentElement().getNodeName() + "	Value: " + nodeList.item(i).getNodeValue());
				}
			} else {
				BlackPublic.errorLog(TAG, "Not Found Expression");
			}
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void insertMyLocationDatabase(ContentValues cv) {
		if (cv == null) {
			BlackPublic.debugLog(TAG, "ContentValues is null");
		}
	}

	public ArrayList<String[]> xpathParserCompile(String msg, String reqExpression) {
		ArrayList<String[]> xpathCompileList = null;
		try {
			Document doc = getDocument(msg);
			XPath xPath = getXpath();
			XPathExpression expr = xPath.compile(reqExpression);
			Object result = expr.evaluate(doc, XPathConstants.NODESET);
			NodeList nodeList = (NodeList) result;
			BlackPublic.debugLog(TAG, "xpathParserCompile: " + msg);
			if (nodeList.getLength() > 0) {
				xpathCompileList = new ArrayList<String[]>();
				BlackPublic.debugLog(TAG, "xpathParserCompile");
				BlackPublic.debugLog(TAG, "xpathParserCompile nodelist.len: " + nodeList.getLength());
				for (int i = 0; i < nodeList.getLength(); i++) {
					BlackPublic.debugLog(TAG, "Type: " + doc.getDocumentElement().getNodeName() + "\nAttValue: " + nodeList.item(i).getAttributes().item(0).getNodeValue() + "\nNodeValue: "
							+ nodeList.item(i).getChildNodes().item(0).getNodeValue());
					String att = nodeList.item(i).getAttributes().item(0).getNodeValue();
					String child = nodeList.item(i).getChildNodes().item(0).getNodeValue();
					xpathCompileList.add(new String[] { att, child });
				}
			} else {
				BlackPublic.errorLog(TAG, "Not Found Expression");
			}

		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return xpathCompileList;
	}

	public String gridXpathParserCompile(String msg, String reqExpression) throws XPathExpressionException {
		String att = null;
		if ((msg == null || msg.equals("")) || (reqExpression == null || reqExpression.equals(""))) {
			BlackPublic.debugLog(TAG, "GridxpathParserCompile msg: " + msg);
			BlackPublic.debugLog(TAG, "GridxpathParserCompile reqExpression: " + reqExpression);
		} else {
			BlackPublic.debugLog(TAG, "Xml Document: " + msg);
			BlackPublic.debugLog(TAG, "Xml reqExpression: " + reqExpression);
			Document doc = getDocument(msg);
			XPath xPath = getXpath();
			XPathExpression expr = xPath.compile(reqExpression);
			Object result = expr.evaluate(doc, XPathConstants.NODESET);
			NodeList nodeList = (NodeList) result;
			if (nodeList.getLength() > 0) {
				BlackPublic.debugLog(TAG, "xpathParserCompile nodelist.len: " + nodeList.getLength());
				for (int i = 0; i < nodeList.getLength(); i++) {
					att = nodeList.item(i).getNodeValue();
					BlackPublic.debugLog(TAG, "Grid : " + att);
				}
			} else {
				BlackPublic.debugLog(TAG, "xpathParserCompile nodelist.len: " + nodeList.getLength());
			}
		}
		return att;
	}

	public ArrayList<String[]> google_xpathParserCompile(String msg, String reqExpression) {
		Log.d("WYJ", "i: " + i++);
		ArrayList<String[]> xpathCompileList = null;
		try {
			Document doc = getDocument(msg);
			XPath xPath = getXpath();
			XPathExpression expr = xPath.compile(reqExpression);
			Object result = expr.evaluate(doc, XPathConstants.NODESET);
			NodeList nodeList = (NodeList) result;
			if (nodeList.getLength() > 0) {
				i = 0;
				Log.d("WYJ", "i1: " + i++);
				xpathCompileList = new ArrayList<String[]>();
				BlackPublic.debugLog(TAG, "xpathParserCompile");
				BlackPublic.debugLog(TAG, "xpathParserCompile nodelist.len: " + nodeList.getLength());
				for (int i = 0; i < nodeList.getLength(); i++) {
					BlackPublic.debugLog(TAG, "Type: " + "	Etc: " + nodeList.item(i).getChildNodes().item(0).getNodeValue());
					String child = nodeList.item(i).getChildNodes().item(0).getNodeValue();
					xpathCompileList.add(new String[] { child });
				}
			} else {
				BlackPublic.errorLog(TAG, "Not Found Expression");
			}

		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return xpathCompileList;
	}
}
